//
//  MainViewController.m
//  滚动视图综合应用
//
//  Created by administrator on 14-9-5.
//  Copyright (c) 2014年 NCS. All rights reserved.
//

#import "MainViewController.h"
#import "ShiTiLei.h"
#import "Cell.h"
#import "DetailViewController.h"
@interface MainViewController (){
    NSMutableArray *shiTi;
    NSArray *images;
    NSIndexPath *selectedIndex;

}

@end

@implementation MainViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *house1=@[@"1.png",@"2.png",@"3.png",@"4.png"];
    NSArray *house2=@[@"5.png",@"6.png",@"7.png",@"8.png"];
    images =@[house1,house2];
    ShiTiLei *shi1=[ShiTiLei shiTiLeiWithDetail:@"房源1" ImageArray:images[0]];
     ShiTiLei *shi2=[ShiTiLei shiTiLeiWithDetail:@"房源2" ImageArray:images[1]];
    shiTi = [NSMutableArray array];
    [shiTi addObject:shi1];
    [shiTi addObject:shi2];
    //给tableView添加手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapGR.numberOfTapsRequired = 1;
    tapGR.numberOfTouchesRequired = 1;
    [self.tableView addGestureRecognizer:tapGR];
    
  
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return shiTi.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Cell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    ShiTiLei *s=shiTi[indexPath.row];
    cell.Label.text=s.detail;
    cell.ImageShow.delegate=self;
    [cell setImageScrollVIew:s.imageArray imageIndex:s.imageIndex];
    //用cell.tag保存当前行号
    cell.tag = indexPath.row;
    
    //用scrollView的tag来保存当前cell的行号,相当于传递的一个参数
    cell.ImageShow.tag = indexPath.row;
    return cell;
}
#pragma mark 调转前传递数据
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"%@",[sender class]);
    Cell *cell = (Cell *)sender;
    NSLog(@"%d",cell.tag);
    
    DetailViewController *detail = segue.destinationViewController;
    detail.detail = cell.Label.text;
}
#pragma mark 单击手势处理方法
-(void)handleTap:(UITapGestureRecognizer *)recognizer{
    //前两种方法失败
    //1
    //    DetailViewController *detail = [[DetailViewController alloc] init];
    //    [self.navigationController pushViewController:detail animated:YES];
    //2
    //    [self presentViewController:detail animated:YES completion:nil];
    
    CGPoint point = [recognizer locationInView:self.tableView];
    NSIndexPath *index = [self.tableView indexPathForRowAtPoint:point];
    Cell *cell = (Cell *)[self.tableView cellForRowAtIndexPath:index];
    
    [self performSegueWithIdentifier:@"Detail" sender:cell];
}
    
#pragma mark scrollView代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //如果滑动是由tableView触发的，则不予以理会
    if ([scrollView isKindOfClass:[self.tableView class]]) return;
    
    NSLog(@"-------");
    
    //获取当前cell的行号
    int row = scrollView.tag;
    
    Cell *cell = (Cell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
    ShiTiLei *s = shiTi[row];
    
    //获取cell上显示图片的index
    int index = s.imageIndex;
    //图片数组count
    int count = s.imageArray.count;
    
    
    
    if (scrollView.contentOffset.x == 640) {
        NSLog(@"向左滑--------");
        
        //调用cell的方法，更新scrollView上的图片,1代表向左滑动
        [cell updateImages:s.imageArray index:index direction:1];
        index++;
        
        //最后再将当前的index赋给cell.imageIndex
        s.imageIndex = index % count;
        
    }
    if (scrollView.contentOffset.x == 0) {
        NSLog(@"向右滑--------");
        if ((index - 2) < 0) {
            index = index + count;
        }
        [cell updateImages:s.imageArray index:index direction:0];
        index--;
        s.imageIndex = index;
    }
    
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"x = %.2f,y = %.2f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

@end
